package edu.umuc.cmsc495_final_project;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;

public class exportDB extends DataBase {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7133333961640149111L;

	static final String RETURN = "\n";
	static final String COMMA = ",";

	JFileChooser fc;
	
	public exportDB(String sFileName) {
		String delims = "[.]";
		String[] splitString = (sFileName.split(delims));
    //check if the filename already has a csv extension  
		if (splitString[splitString.length-1].contentEquals("csv")) {
			generateCsvFile(sFileName);
		} else {generateCsvFile(sFileName + ".csv");}
	}

	private void generateCsvFile(String sFileName) {
		try {
			FileWriter fstream = new FileWriter(sFileName);
			BufferedWriter writer = new BufferedWriter(fstream);

			writer.append("Restaurant Database *.csv " + sFileName);

			writer.append(RETURN);

			// Restaurant info
			writer.append("Database name");
			writer.append(COMMA);
			writer.append(dbName);
			writer.append(COMMA);

			writer.append(RETURN);

			writer.append("Date Created");
			writer.append(COMMA);
			writer.append(dateCreated.toString());
			writer.append(COMMA);

			writer.append(RETURN);

			writer.append("Hours of Operation");
			writer.append(COMMA);
			writer.append("Open Time");
			writer.append(COMMA);
			writer.append(hoursOfOpOpen.hoursOfOp());
			writer.append("Close Time");
			writer.append(COMMA);
			writer.append(hoursOfOpClose.hoursOfOp());

			writer.append(RETURN);

			writer.append("Current Tax Rate");
			writer.append(COMMA);
			writer.append(String.valueOf(taxRate));

			writer.append(RETURN);

			writer.append("Notes");
			writer.append(COMMA);
			writer.append(notes1);
			writer.append(COMMA);
			writer.append(notes2);
			writer.append(COMMA);
			writer.append(notes3);

			writer.append(RETURN);

			// employee info
			writer.append("Employees Info");
			writer.append(COMMA);
			writer.append("There are ");
			writer.append(String.valueOf(employees.size()));
			writer.append(" employees");
			writer.append(RETURN);

			// add employees
			for (int i = 0; i < employees.size(); i++) {
				writer.append(i + " - Last name: ");
				writer.append(employees.get(i).getLastname());
				writer.append(COMMA);

				writer.append("First name: ");
				writer.append(employees.get(i).getFirstname());
				writer.append(COMMA);

				writer.append("ID: ");
				writer.append(String.valueOf(employees.get(i).getIdNum()));
				writer.append(COMMA);

				writer.append("Credential: ");
				writer.append(String.valueOf(employees.get(i).getPrivelageLevel()));

				writer.append(RETURN);
			}

			// add tables and bar stools
			writer.append("There are " + tables.size() + " tables.");
			writer.append(COMMA);
			for(int i=0; i<tables.size(); i++){
				writer.append(String.valueOf(tables.get(i).getTableType()));
				writer.append(COMMA);
				
				writer.append(RETURN);
			}

			writer.append("There are " + tables.size() + " barstools.");
			writer.append(COMMA);
			for(int i=0; i<barStools.size(); i++){
				writer.append(String.valueOf(barStools.get(i).getTableType()));
				writer.append(COMMA);
				
				writer.append(RETURN);
			}


			// patron info
			writer.append("Current & Past Patron Info");
			writer.append(COMMA);
			writer.append("There have been ");
			writer.append(String.valueOf(patrons.size()));
			writer.append(" patrons");
			writer.append(RETURN);

			// add patrons
			for (int i = 0; i < patrons.size(); i++) {
				writer.append(i + " - Reservation name: ");
				writer.append(patrons.get(i).getReservationName());
				writer.append(COMMA);

				writer.append("Dine Type: ");
				writer.append(String.valueOf( patrons.get(i).getDineType()));
				writer.append(COMMA);

				writer.append("Number in party: ");
				writer.append(String.valueOf(patrons.get(i).getNumInParty()));
				writer.append(COMMA);

				writer.append("Ticket is ");
				if(!patrons.get(i).isTicketClosed()){
					writer.append("open");
					writer.append(COMMA);

					writer.append("Checked in ");
					writer.append(String.valueOf(patrons.get(i).getCheckinTime()));
					writer.append(COMMA);
					
					writer.append("Started eating at ");
					writer.append(String.valueOf(patrons.get(i).getStartTime()));
					writer.append(COMMA);
					
					writer.append("Patron is ");
					if(patrons.get(i).isSeated()){
						writer.append("seated");
						
						// what table
						writer.append(COMMA);
						writer.append(String.valueOf(patrons.get(i).getTable()));
						writer.append(COMMA);
						
						// order
						writer.append("Order details");
						writer.append(COMMA);
						for(int j=0; j<patrons.get(i).getTicket().size(); j++){
							writer.append(String.valueOf(patrons.get(i).getTicket().get(j)));//TODO complete this phrase so it has ticket items
							writer.append(COMMA);
						}
					}
					else{
						writer.append("not seated yet");
						writer.append(COMMA);
					}
						
				}
				else{
					writer.append("closed");
					writer.append(COMMA);

					writer.append("Checked in ");
					writer.append(String.valueOf(patrons.get(i).getCheckinTime()));
					writer.append(COMMA);
					
					writer.append("Started eating at ");
					writer.append(String.valueOf(patrons.get(i).getStartTime()));
					writer.append(COMMA);
					
					writer.append("Finsished eating at ");
					writer.append(String.valueOf(patrons.get(i).getFinishTime()));
					writer.append(COMMA);
					
					// what table
					writer.append(COMMA);
					writer.append(String.valueOf(patrons.get(i).getTable()));
					writer.append(COMMA);
					
					// order
					writer.append("Order details");
					writer.append(COMMA);
					for(int j=0; j<patrons.get(i).getTicket().size(); j++){
						writer.append(String.valueOf(patrons.get(i).getTicket().get(j)));//TODO complete this phrase so it has ticket items
						writer.append(COMMA);
					}
				}
				
				writer.append(RETURN);
			}

			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}